package com.salon.modules.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salon.common.exception.CustomException;
import com.salon.common.http.response.PageResult;
import com.salon.common.utils.Query;
import com.salon.common.utils.RedisKeys;
import com.salon.common.utils.RedisUtils;
import com.salon.modules.sys.entity.SysConfigEntity;
import com.salon.modules.sys.mapper.SysConfigMapper;
import com.salon.modules.sys.service.ISysConfigService;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("sysConfigService")
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfigEntity>
        implements ISysConfigService {

    @Resource
    private RedisUtils redisUtils;

    @Override
    public PageResult<SysConfigEntity> listByPage(Map<String, Object> params) {
        IPage<SysConfigEntity> pageParams = new Query<SysConfigEntity>().getPage(params);
        String paramKey = (String) params.get("paramKey");
        LambdaQueryWrapper<SysConfigEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(paramKey), SysConfigEntity::getParamKey, paramKey);
        queryWrapper.eq(SysConfigEntity::getStatus, 1);
        IPage<SysConfigEntity> page = this.page(pageParams, queryWrapper);
        List<SysConfigEntity> resultList = page.getRecords();
        return new PageResult<>(page.getTotal(), page.getCurrent(), page.getSize(), resultList);
    }

    @Override
    public void saveConfig(SysConfigEntity config) {
        this.save(config);
        if (config == null) {
            return;
        }
        redisUtils.set(RedisKeys.getSysConfigKey(config.getParamKey()), JSON.toJSONString(config));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysConfigEntity config) {
        this.updateById(config);
        if (config == null) {
            return;
        }
        redisUtils.set(RedisKeys.getSysConfigKey(config.getParamKey()), JSON.toJSONString(config));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            SysConfigEntity config = this.getById(id);
            redisUtils.del(RedisKeys.getSysConfigKey(config.getParamKey()));
        }
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public String getValue(String key) {
        SysConfigEntity config = null;
        Object oldValue = redisUtils.get(RedisKeys.getSysConfigKey(key));
        if (ObjectUtils.isNotEmpty(oldValue)) {
            config = JSON.parseObject(String.valueOf(oldValue), SysConfigEntity.class);
        }
        if (config == null) {
            config = baseMapper.getConfigInfoByKey(key);
            String newRedisKey = RedisKeys.getSysConfigKey(config.getParamKey());
            redisUtils.set(newRedisKey, JSON.toJSONString(config));
        }
        if (StringUtils.isEmpty(config.getParamValue())) {
            return null;
        } else {
            return config.getParamValue();
        }
    }

    @Override
    public <T> T getConfigObject(String key, Class<T> clazz) {
        try {
            String value = getValue(key);
            if (StringUtils.isNotBlank(value)) {
                return JSON.parseObject(value, clazz);
            }
            return clazz.newInstance();
        } catch (Exception e) {
            throw new CustomException("获取参数失败");
        }
    }

}
